package com.ozo.easy.common.auth;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;

import java.util.Collections;
import java.util.Set;

/**
 * 用户登录工具类
 *
 * @author ozo
 * @date 2024-12-26
 */
public class UserLoginHelper {

    public static final String LOGIN_USER = "loginUser";

    public static final String USER_ID = "userId";

    public static final String USER_NAME = "username";

    public static final String NICK_NAME = "nickname";

    public static final String REAL_NAME = "realName";

    public static final String GENDER = "gender";

    public static final String DEPT_ID = "deptId";

    public static final String DEPT_NAME = "deptName";

    /**
     * 登录
     *
     * @param loginUser
     * @param model
     */
    public static void login(LoginUser loginUser, SaLoginModel model) {
        model = ObjectUtil.defaultIfNull(model, new SaLoginModel());
        model.setExtra(USER_ID, loginUser.getUserId())
                .setExtra(USER_NAME, loginUser.getUsername());
        model.setIsLastingCookie(false);
        StpUtil.login(loginUser.getUserId(), model);
        StpUtil.getTokenSession().set(LOGIN_USER, loginUser);
    }

    /**
     * 退出登录
     */
    public static void logout() {
        StpUtil.logout();
    }

    /**
     * 获取token
     */
    public static SaTokenInfo getTokenInfo() {
        return StpUtil.getTokenInfo();
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public static Long getUserId() {
        Long userId = null;
        try {
            userId = StpUtil.getLoginIdAsLong();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userId;
    }

    /**
     * 获取用户名
     *
     * @return
     */
    public static String getUsername() {
        LoginUser loginUser = getLoginUser();
        if (loginUser == null) {
            return null;
        }
        return loginUser.getUsername();
    }

    /**
     * 获取角色
     *
     * @return
     */
    public static Set<String> getRoles() {
        LoginUser loginUser = getLoginUser();
        if (loginUser == null) {
            return Collections.emptySet();
        }
        return loginUser.getRoles();
    }

    /**
     * 获取权限
     *
     * @return
     */
    public static Set<String> getPermissions() {
        LoginUser loginUser = getLoginUser();
        if (loginUser == null) {
            return Collections.emptySet();
        }
        return loginUser.getPermissions();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static LoginUser getLoginUser() {
        SaSession session = null;
        try {
            session = StpUtil.getTokenSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ObjectUtil.isNull(session)) {
            return null;
        }
        return (LoginUser) session.get(LOGIN_USER);
    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    public static LoginUser getLoginUserByToken(String token) {
        SaSession session = StpUtil.getTokenSessionByToken(token);
        if (ObjectUtil.isNull(session)) {
            return null;
        }
        return (LoginUser) session.get(LOGIN_USER);
    }

}